package com.serviceprovider.business.module.order.model;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.order.valueobjects.OrderFilterConditionVO;
import com.serviceprovider.business.module.order.valueobjects.OrderFilterVO;

//~--- JDK imports ------------------------------------------------------------

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderFilterConditionForm {
    private boolean       isActive              = true;
    private ConditionType dateToConditionType   = ConditionType.IS;
    private ConditionType dateFromConditionType = ConditionType.IS;
    private Date          dateFrom;
    private Date          dateTo;
    private Integer       id;
    private ConditionType providerConditionType;
    private Integer       providerId;
    private String        state;
    private ConditionType stateConditionType;
    private ConditionType statusConditionType;
    private Integer       statusId;
    private String        title;

    public OrderFilterConditionForm() {}

    public OrderFilterConditionForm(OrderFilterVO vo) {
        this.id       = vo.getId();
        this.title    = vo.getName();
        this.isActive = vo.isActive();

        List<OrderFilterConditionVO> list = vo.getFilterConditionList();

        for (OrderFilterConditionVO item : list) {
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

            switch (item.getOrderConditionField()) {
            case PROVIDER :
                this.providerConditionType = item.getConditionType();
                this.providerId            = Integer.valueOf(item.getConditionValue());

                break;

            case DATE_FROM :
                this.dateFromConditionType = item.getConditionType();

                try {
                    this.dateFrom = format.parse(item.getConditionValue());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }

                break;

            case DATE_TO :
                this.dateToConditionType = item.getConditionType();

                try {
                    this.dateTo = format.parse(item.getConditionValue());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }

                break;

            case STATE :
                this.stateConditionType = item.getConditionType();
                this.state              = item.getConditionValue();

                break;

            case STATUS :
                this.statusConditionType = item.getConditionType();
                this.statusId            = Integer.valueOf(item.getConditionValue());

                break;
            }
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        isActive = active;
    }

    public Integer getProviderId() {
        return providerId;
    }

    public void setProviderId(Integer providerId) {
        this.providerId = providerId;
    }

    public ConditionType getProviderConditionType() {
        return providerConditionType;
    }

    public void setProviderConditionType(ConditionType providerConditionType) {
        this.providerConditionType = providerConditionType;
    }

    public Date getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(Date dateFrom) {
        this.dateFrom = dateFrom;
    }

    public ConditionType getDateFromConditionType() {
        return dateFromConditionType;
    }

    public void setDateFromConditionType(ConditionType dateFromConditionType) {
        this.dateFromConditionType = dateFromConditionType;
    }

    public Date getDateTo() {
        return dateTo;
    }

    public void setDateTo(Date dateTo) {
        this.dateTo = dateTo;
    }

    public ConditionType getDateToConditionType() {
        return dateToConditionType;
    }

    public void setDateToConditionType(ConditionType dateToConditionType) {
        this.dateToConditionType = dateToConditionType;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public ConditionType getStateConditionType() {
        return stateConditionType;
    }

    public void setStateConditionType(ConditionType stateConditionType) {
        this.stateConditionType = stateConditionType;
    }

    public Integer getStatusId() {
        return statusId;
    }

    public void setStatusId(Integer statusId) {
        this.statusId = statusId;
    }

    public ConditionType getStatusConditionType() {
        return statusConditionType;
    }

    public void setStatusConditionType(ConditionType statusConditionType) {
        this.statusConditionType = statusConditionType;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public OrderFilterVO convertToVO() {
        OrderFilterVO result = new OrderFilterVO();

        result.setActive(this.isActive);
        result.setId(this.id);
        result.setName(this.getTitle());

        List<OrderFilterConditionVO> list   = new ArrayList<OrderFilterConditionVO>();
        SimpleDateFormat             format = new SimpleDateFormat("MM/dd/yyyy");

        if (providerId != 0) {
            OrderFilterConditionVO vo = new OrderFilterConditionVO();

            vo.setOrderConditionField(OrderConditionField.PROVIDER);
            vo.setConditionType(providerConditionType);
            vo.setConditionValue(providerId.toString());
            list.add(vo);
        }

        if (dateFrom != null) {
            OrderFilterConditionVO vo = new OrderFilterConditionVO();

            vo.setOrderConditionField(OrderConditionField.DATE_FROM);
            vo.setConditionType(dateFromConditionType);
            vo.setConditionValue(format.format(dateFrom));
            list.add(vo);
        }

        if (dateTo != null) {
            OrderFilterConditionVO vo = new OrderFilterConditionVO();

            vo.setOrderConditionField(OrderConditionField.DATE_TO);
            vo.setConditionType(dateToConditionType);
            vo.setConditionValue(format.format(dateTo));
            list.add(vo);
        }

        if (state != null) {
            OrderFilterConditionVO vo = new OrderFilterConditionVO();

            vo.setOrderConditionField(OrderConditionField.STATE);
            vo.setConditionType(stateConditionType);
            vo.setConditionValue(state.toString());
            list.add(vo);
        }

        if (statusId != 0) {
            OrderFilterConditionVO vo = new OrderFilterConditionVO();

            vo.setOrderConditionField(OrderConditionField.STATUS);
            vo.setConditionType(statusConditionType);
            vo.setConditionValue(new Integer(statusId).toString());
            list.add(vo);
        }

        result.setFilterConditionList(list);

        return result;
    }
}



